package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcCsunBodyGroupBind;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.dto.bodysensor.BodyGroupDto;
import com.csun.cmny.provider.model.dto.bodysensor.BodyGroupQueryDto;
import com.csun.cmny.provider.model.dto.device.DmcEditDeviceDto;
import com.csun.cmny.provider.model.dto.humidistat.BindCsunHumidistatLocationDto;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.csunbody.BodyGroupBindVo;
import com.csun.cmny.provider.model.vo.csunbody.BodyGroupVo;
import com.csun.cmny.provider.model.vo.rfid.DmcRfidReaderVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcCsunBodyGroupBindService;
import com.csun.cmny.provider.service.DmcCsunBodyGroupService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-09-22 15:51
 * @Description:
 */
@RestController
@RequestMapping(value = "/bodyGroup", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcCsunBodyGroupController extends BaseController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcCsunBodyGroupService dmcCsunBodyGroupService;
    @Resource
    private DmcCsunBodyGroupBindService dmcCsunBodyGroupBindService;

    /**
     * 保存
     */
    @ValidateAnnotation
    @PostMapping(value = "/save")
    public Wrapper add(@Validated @RequestBody BodyGroupDto bodyGroupDto, BindingResult br) {

        Integer result = dmcCsunBodyGroupService.save(bodyGroupDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),result);
    }


    /**
     * 查询
     */
    @PostMapping(value = "/queryListWithPage")
    public Wrapper queryListWithPage(@RequestBody BodyGroupQueryDto bodyGroupQueryDto) {

        bodyGroupQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        List<BodyGroupVo> bodyGroupVoList = dmcCsunBodyGroupService.queryListWithPage(bodyGroupQueryDto);

        for (BodyGroupVo v : bodyGroupVoList) {
            String timeKey = RedisKeyUtil.getCsunBodyGroupKey(v.getId());
            BodyGroupVo bodyGroupVo = (BodyGroupVo) redisTemplate.opsForValue().get(timeKey);
            if (PublicUtil.isNotEmpty(bodyGroupVo)) {
                v.setTouch(bodyGroupVo.getTouch());
                v.setTouchDate(bodyGroupVo.getTouchDate());
                v.setDeviceNo(bodyGroupVo.getDeviceNo());
            } else {
                v.setTouchDate("");
                v.setDeviceNo("");
            }
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), new PageInfo<>(bodyGroupVoList));
    }

    /**
     * 删除
     */
    @PostMapping(value = "/deleteById/{id}")
    public Wrapper deleteById(@PathVariable Long id) {

        Integer result = dmcCsunBodyGroupService.deleteById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 详情
     */
    @PostMapping(value = "/queryListById/{id}")
    public Wrapper queryListById(@PathVariable Long id) {

        List<BodyGroupBindVo> bodyGroupBindVoList = dmcCsunBodyGroupBindService.queryListById(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), bodyGroupBindVoList);
    }

    /**
     * 绑定
     */
    @PostMapping(value = "/bind")
    public Wrapper bind(@RequestBody DmcCsunBodyGroupBind dmcCsunBodyGroupBind) {

        Integer result = dmcCsunBodyGroupBindService.bind(dmcCsunBodyGroupBind, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 解绑
     */
    @PostMapping(value = "/untied")
    public Wrapper untied(@RequestBody DmcCsunBodyGroupBind dmcCsunBodyGroupBind) {

        Integer result = dmcCsunBodyGroupBindService.untied(dmcCsunBodyGroupBind);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }
}
